package com.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.model.User;
import com.service.service.UserMapper;
import com.service.service.UserinfoService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class UserServiceImpl implements UserinfoService {
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Override
    public void save(User user) {
        sqlSessionFactory.openSession().insert("User.save", user);

        System.out.print("1111111");
    }

    @Override
    public boolean update(User user) {
        int isOk = sqlSessionFactory.openSession().update("User.update", user);
        return isOk > 0 ? true : false;
    }

    @Override
    public boolean delete(int id) {
        return false;
    }

    @Override
    public User findById(int id) {
        User user = sqlSessionFactory.openSession().selectOne("User.findById", id);
        return user;
    }

    @Override
    public List<User> findAll() {
        List<User> list = sqlSessionFactory.openSession().selectList("User.findAll");
        return list;
    }

    //测试sql
    public List<Map> selectForTestSQL() {
        List<Map> list = sqlSessionFactory.openSession().selectList("User.selectForTestSQL");
        return list;
    }

    //测试ResultMap

    public List<User> selectForTestResultMap() {
        List<User> list = sqlSessionFactory.openSession().selectList("User.selectForTestMap");
        System.out.println("测试ResultMap数据：----------------------》》》");
        for (User user : list){
          System.out.println(user.getUserName());
        }
        return list;
    }
}
